#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###----------1、文件说明----------###
'''
* 说明：窗口模板
* 时间：2024-11-02 15:57:27
* 文件：
* 作者：Smile
* 版本：0.1
* 备注：
'''
import os, sys
from PySide2.QtWidgets import QMainWindow, QApplication, QWidget
from PySide2.QtWidgets import QLineEdit, QPushButton, QTextEdit
from PySide2.QtWidgets import QHBoxLayout, QVBoxLayout
# 文件选择函数所需要的库
from PySide2.QtWidgets import QFileDialog,QDialog
import qdarkstyle
from qdarkstyle import LightPalette # 颜色主题
import pywinstyles  # 窗口样式
import qtawesome as qta # 图标库
from loguru import logger as log
import json

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle("界面例程")
        self.setStyleSheet(qdarkstyle.load_stylesheet(qt_api='pyqt5', palette=LightPalette())  )
        self.initGUI()
        self.setWindowIcon(qta.icon('fa5s.yin-yang', color='brown', color_active='orange'))
        self.path = []

    def initGUI(self):
        self.widget =  QWidget(self)
        layout = QVBoxLayout(self.widget)
        # 第一行
        self.lineedit = QLineEdit(self.widget)
        self.button = QPushButton("选择目录", self.widget)
        linelayout = QHBoxLayout(self.widget)
        linelayout.addWidget(self.lineedit)
        linelayout.addWidget(self.button)
        # 第二行
        self.textedit = QTextEdit(self.widget)
        layout.addLayout(linelayout)
        layout.addWidget(self.textedit)
        # 第三行
        self.startbutton = QPushButton("运行程序", self.widget)
        layout.addWidget(self.startbutton)
        # 设置主要显示区
        self.setCentralWidget(self.widget)

        self.button.clicked.connect(self.buttonCallback)
        self.startbutton.clicked.connect(self.run)

    def fileSelect(self, filemode=0):
        '''
        filemode: 0-文件的名称，无论它是否存在 1-单文件模式 2-目录模型 3-多文件模式
        '''
        fileNames = []
        fileDialog = QFileDialog(self)
        fileDialog.setViewMode(QFileDialog.Detail)

        if filemode == 1:
            fileDialog.setFileMode(QFileDialog.ExistingFile)
        elif filemode == 2:
            fileDialog.setFileMode(QFileDialog.Directory)
        elif filemode == 3:
            fileDialog.setFileMode(QFileDialog.ExistingFiles)
        else:
            fileDialog.setFileMode(QFileDialog.AnyFile)

        fileDialog.setNameFilter("python file(*.py);;all file(*)")
        ret = fileDialog.exec_()
        if ret == QDialog.Accepted:
            fileNames = fileDialog.selectedFiles()
        return fileNames

    def buttonCallback(self, evt):
        print(self.lineedit.text())
        self.path = self.fileSelect(2)
        self.lineedit.setText(self.path[0])

    @log.catch  # 捕获报错信息
    def run(self):
        log.info("Start Program")
        path = self.lineedit.text()

        for name in os.listdir(path):
            # print(name)
            jsonpath = os.path.join(path, name, "manifest.json")
            with open(jsonpath, "r", encoding="utf-8") as f:
                manifest = json.load(f)
                print(manifest["id"], "::", manifest["version"])

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    pywinstyles.apply_style(mainWin, "win7")
    mainWin.show()
    sys.exit(app.exec_())
